﻿'Author: Justin Bassett
'Date: 1/6/09

Imports System.IO

Public Class frmMultiplicationPractice

    Public HighScore(4, 2) As String
    Dim multiplier As Double

    Private Sub mnuAbout_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnuAbout.Click
        frmAboutMultiplication.Show()
    End Sub

    Private Sub mnuExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnuExit.Click
        Application.Exit()
    End Sub

    Private Sub Load_High_Scores()
        Try
            Dim fs As New FileStream("HighScores.txt", FileMode.Open, FileAccess.Read)
            Dim scoresStream As New StreamReader(fs)
            Dim stringScores As String = Nothing
            Do While scoresStream.Peek <> -1
                stringScores &= ChrW(scoresStream.Read())
            Loop
            fs.Close()
            scoresStream.Close()
            For i As Integer = 0 To 4
                For j As Integer = 0 To 2
                    Dim lastIndex As Integer = stringScores.IndexOf(",")
                    HighScore(i, j) = stringScores.Substring(0, lastIndex)
                    stringScores = stringScores.Remove(0, lastIndex + 1)
                    'MessageBox.Show(HighScore(i, j), i & "," & j)
                Next j
            Next i
        Catch
            MessageBox.Show("Error when loading Scores!", Me.Text, MessageBoxButtons.OK, MessageBoxIcon.Error)
        End Try
    End Sub

    Private Sub Save_High_Scores()
        Try
            Dim fs As New FileStream("HighScores.txt", FileMode.Create, FileAccess.Write)
            Dim scoresStream As New StreamWriter(fs)
            Dim stringScores As String = Nothing
            For i As Integer = 0 To 4
                For j As Integer = 0 To 2
                    stringScores &= HighScore(i, j) + ","
                Next j
            Next i
            scoresStream.Write(stringScores)
            scoresStream.Flush()
            scoresStream.Close()
            fs.Close()
        Catch
            MessageBox.Show("Error when saving Scores!", Me.Text, MessageBoxButtons.OK, MessageBoxIcon.Error)
        End Try
    End Sub

    Private Sub frmMultiplicationPractice_FormClosed(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosedEventArgs) Handles Me.FormClosed
        Save_High_Scores()
    End Sub

    Private Sub frmMultiplicationPractice_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Randomize()
        Load_High_Scores()
    End Sub

    Private Sub mnuHighScore_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnuHighScore.Click
        frmHighScores.Show()
    End Sub

    Private Sub cboLevel_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cboLevel.SelectedIndexChanged
        'MessageBox.Show(Me.cboLevel.SelectedIndex())
        Select Case Me.cboLevel.SelectedIndex()
            Case 0
                multiplier = 0.75
            Case 1
                multiplier = 1
            Case 2
                multiplier = 1.5
            Case 3
                multiplier = 2
        End Select
        Me.lblLevelMultiplier.Text = "Level muiltiplier: " & multiplier
    End Sub

    Private Sub btnStart_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnStart.Click
        'Error checking code and starting up the game here
        'calls frmGame.vb
        Do While Me.txtUserNme.Text = Nothing
            Me.txtUserNme.Text = InputBox("Enter a valid user name:", Me.Text)
        Loop
        Do While lstStudy.SelectedIndex() = -1
            'MessageBox.Show(lstStudy.SelectedIndex)
            MessageBox.Show("Please select an area of study!", Me.Text)
            Exit Sub
        Loop
        Do While Me.cboLevel.SelectedIndex = -1
            MessageBox.Show("Please select a level!", Me.Text)
            Exit Sub
        Loop
        If lstStudy.SelectedIndex() = 3 Then
            If MessageBox.Show("All answers must be to 3 decimal places", "Warning", MessageBoxButtons.OKCancel) = 2 Then 'If cancel is click will not start game
                Exit Sub
            End If
        End If
        frmGame.passInformations(Me.txtUserNme.Text, multiplier, lstStudy.SelectedIndex())
        frmGame.Show()
        Me.Hide()
    End Sub

    Private Sub mnuDirections_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles mnuDirections.Click
        frmDirections.Show()
    End Sub
End Class